/**
    Copyright (C) 2011  Alexander Vollmer

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package org.jefb.web.controller;

import java.io.Serializable;
import java.util.Iterator;
import java.util.Set;

import org.jefb.entity.RoleEntity;
import org.jefb.sec.entity.UserRight;
import org.jefb.service.IRoleService;
import org.jefb.web.model.EntityModel;
import org.jefb.web.model.RoleModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.event.Event;
import org.zkoss.zk.ui.event.EventListener;
import org.zkoss.zk.ui.event.Events;
import org.zkoss.zk.ui.event.SelectEvent;
import org.zkoss.zul.Grid;
import org.zkoss.zul.Listbox;
import org.zkoss.zul.Listitem;

@org.springframework.stereotype.Component("roleController")
@Scope("prototype")
public class RoleController extends EntityController<RoleEntity> implements
		Serializable {

	private static final long serialVersionUID = 1L;

	// services
	@Autowired
	private IRoleService roleService;

	@Autowired
	private RoleModel roleModel;

	protected Grid permissionsGrid;

	// models

	// UI components
	protected Listbox assignedPermissionsListbox;
	protected Listbox unassignedPermissionsListbox;

	private String[] colIds = new String[] { "authority", "description" };

	@Override
	protected String[] getColumnIds() {
		return colIds;
	}

	@Override
	protected RoleEntity createNewEntityInstance() {
		return new RoleEntity();
	}

	@Override
	public void doAfterCompose(Component comp) throws Exception {
		super.doAfterCompose(comp);
		unassignedPermissionsListbox.addEventListener(Events.ON_SELECT,
				new EventListener() {
					public void onEvent(Event event) throws Exception {
						if (event instanceof SelectEvent) {
							SelectEvent selectEvent = (SelectEvent) event;
							Set<?> selectedItems = selectEvent
									.getSelectedItems();
							Iterator<?> iterator = selectedItems.iterator();
							while (iterator.hasNext()) {
								Object next = iterator.next();
								if (next instanceof Listitem) {
									Listitem listitem = (Listitem) next;
									UserRight value = (UserRight) listitem
											.getValue();
									if (addModeEnabled) {
										getModel().getNewEntity().getRights()
												.add(value);
									} else {
										getModel().getSelectedEntity()
												.getRights().add(value);
									}
								}
							}
						}
					}
				});
		assignedPermissionsListbox.addEventListener(Events.ON_SELECT,
				new EventListener() {
					public void onEvent(Event event) throws Exception {
						if (event instanceof SelectEvent) {
							SelectEvent selectEvent = (SelectEvent) event;
							Set<?> selectedItems = selectEvent
									.getSelectedItems();
							Iterator<?> iterator = selectedItems.iterator();
							while (iterator.hasNext()) {
								Object next = iterator.next();
								if (next instanceof Listitem) {
									Listitem listitem = (Listitem) next;
									UserRight value = (UserRight) listitem
											.getValue();
									if (addModeEnabled) {
										getModel().getNewEntity().getRights()
												.remove(value);
									} else {
										getModel().getSelectedEntity()
												.getRights().remove(value);
									}
								}
							}
						}
					}
				});
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.jefb.web.controller.EntityController#setAddMode()
	 */
	@Override
	protected void setAddMode() {
		super.setAddMode();
		permissionsGrid.setVisible(true);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.jefb.web.controller.EntityController#setEditMode()
	 */
	@Override
	protected void setEditMode() {
		super.setEditMode();
		permissionsGrid.setVisible(true);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.jefb.web.controller.EntityController#resetModel()
	 */
	@Override
	protected void resetModel() {
		super.resetModel();
		if (permissionsGrid != null) {
			permissionsGrid.setVisible(false);
		}
	}

	public void setRoleService(IRoleService roleService) {
		this.roleService = roleService;
	}

	public IRoleService getRoleService() {
		return roleService;
	}

	public void setRoleModel(RoleModel roleModel) {
		this.roleModel = roleModel;
	}

	public RoleModel getRoleModel() {
		return roleModel;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.jefb.web.controller.EntityController#getModel()
	 */
	@Override
	public EntityModel<RoleEntity> getModel() {
		return roleModel;
	}

}
